package com.yict.common.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

/**
 * <h3>配置文件</h3> <h4>配置文件的基本操作</h4>
 * 
 * @ver 0.1
 * @author Jay.Wu 2008-7-15
 */
@Service
public class EnvironmentUtil {

	protected transient final Log log = LogFactory.getLog(getClass());

	private Properties propertie;

	private InputStream inputFile;

	private FileOutputStream outputFile;

	private String filePath;

	static EnvironmentUtil ec;

	public EnvironmentUtil() {
		// this(Constants.ENVIRONMENT_PROPERTIES_PATH);
	}

	public EnvironmentUtil(String filePath) {
		propertie = new Properties();
		setProperties(filePath);
		this.filePath = filePath;
	}

	/**
	 * 获得唯一的EnvironmentUtil实例
	 * 
	 * @return
	 */
	public static EnvironmentUtil getInstance() {
		if (ec == null)
			ec = new EnvironmentUtil();
		return ec;
	}

	public Properties getProperties() {
		return propertie;
	}

	/**
	 * 装载配置文件
	 * 
	 * @param filePath
	 *            要读取的配置文件的路径+名称
	 */
	public void setProperties(String filePath) {
		try {
			try {
				inputFile = new FileInputStream(filePath);
			} catch (Exception e) {
				inputFile = this.getClass().getClassLoader().getResourceAsStream(filePath);
			}

			propertie.load(inputFile);
			inputFile.close();
		} catch (Exception e) {
			log.error("", e);
		}
	}

	/** */
	/**
	 * 获得键值
	 * 
	 * @param key
	 *            取得其值的键
	 * @return key的值
	 */
	public String getPropertyValue(String key) {
		if (propertie.containsKey(key)) {
			// 得到某一属性的值
			String value = propertie.getProperty(key);
			return value;
		} else
			return "";
	}

	/**
	 * 直接通过指定文件获得相应的键值
	 * 
	 * @param fileName
	 * @param key
	 * @return
	 */
	public String getPropertyValue(String fileName, String key) {
		setProperties(fileName);
		String value = "";

		if (propertie.containsKey(key))
			value = propertie.getProperty(key);

		return value;
	}

	/**
	 * 清除properties文件中所有的key和其值
	 */
	public void clear() {
		propertie.clear();
	}

	/**
	 * 改变或添加一个key的值，当key存在于properties文件中时该key的值被value所代替， 当key不存在时，该key的值是value
	 * 
	 * @param key
	 *            要存入的键
	 * @param value
	 *            要存入的值
	 */
	public void setValue(String key, String value) {
		propertie.setProperty(key, value);
	}

	/**
	 * 将更改后的文件数据存入指定的文件中，该文件可以事先不存在。
	 * 
	 * @param fileName
	 *            文件路径+文件名称
	 * @param description
	 *            对该文件的描述
	 */
	public void saveFile(String fileName, String description) {
		try {
			outputFile = new FileOutputStream(fileName);
			propertie.store(outputFile, description);
			outputFile.flush();
			outputFile.close();
		} catch (Exception e) {
			log.error("", e);
		}
	}

	public void saveFile() {
		String fileName = ClassLoaderUtil.getAbsolutePathOfClassLoaderClassPath() + filePath;
		fileName = fileName.replaceAll("file:/", "");
		System.out.println("-------------saveFile(): " + fileName);

		try {
			outputFile = new FileOutputStream(fileName);
			propertie.store(outputFile, "");
			outputFile.close();
		} catch (Exception e) {
			log.error("", e);
		}
	}

	public static void main(String[] args) {
		System.out.println(ClassLoaderUtil.getAbsolutePathOfClassLoaderClassPath());
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
}
